package com.zy.ivideo.service;

import com.zy.ivideo.entity.AccountEntity;
import com.zy.ivideo.entity.BankCardEntity;
import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.repository.AccountRepository;
import com.zy.ivideo.repository.BankCardRepository;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class AccountService {
    @Autowired
    AccountRepository accountRepository;
    @Autowired
    BankCardRepository bankCardRepository;

    /**
     * 添加 账户 信息
     * @param entity
     * @return
     */
    public ResponseEntity<AccountEntity> addAccount(AccountEntity entity){
        AccountEntity result = accountRepository.save(entity);
        if (result!=null){
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("添加账户信息失败");
    }

    /**
     * 根据id删除账户信息
     * @param id
     * @return
     */
    public ResponseEntity<Boolean> removeAccountById(Long id){
        try{
            accountRepository.deleteById(id);
            return ResponseUtils.success(true);
        }
        catch (Exception ex){
            return ResponseUtils.failed("删除账户信息失败，error:"+ex.getMessage());
        }
    }

    /**
     * 根据账号id获取对应账户信息
     * @param id
     * @return
     */
    public ResponseEntity<AccountEntity> findAccountById(Long id){
        Optional<AccountEntity> result = accountRepository.findById(id);
        if (result!=null&&result.get()!=null){
            AccountEntity accountEntity = result.get();
            List<BankCardEntity> list = bankCardRepository.findBankCardEntitiesByAccountid(id);

            accountEntity.setCards(list);
            return ResponseUtils.success(accountEntity);
        }
        return ResponseUtils.failed("没有查找到该账户信息");
    }

    /**
     * 根据账户id获取对应账户信息
     * @param userid
     * @return
     */
    public ResponseEntity<AccountEntity> findAccountByUserId(Long userid){
        AccountEntity result = accountRepository.findAccountEntityByUserid(userid);
        if (result!=null){
            List<BankCardEntity> cards = bankCardRepository.findBankCardEntitiesByAccountid(result.getId());
            result.setCards(cards);
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("没有获取到对应账户信息");
    }

    /**
     * 修改账户信息
     * @param entity
     * @return
     */
    public ResponseEntity<AccountEntity> modifyAccount(AccountEntity entity){
        AccountEntity result = accountRepository.save(entity);
        if (result!=null){
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("修改账户信息");
    }

    /**
     * 修改支付宝账号
     * @param id
     * @param zfb
     * @return
     */
    public ResponseEntity<Boolean> modifyZFB(Long id,String zfb){
        int result = accountRepository.modifyZFB(id, zfb);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("修改支付宝信息失败");
    }


    /**
     * 修改微信账号信息
     * @param id
     * @param wx
     * @return
     */
    public ResponseEntity<Boolean> modifyWX(Long id,String wx){
        int result = accountRepository.modifyWX(id, wx);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("修改微信账号信息失败");
    }

    /**
     * 修改账户金额
     * @param id
     * @param nm
     * @return
     */
    public ResponseEntity<Boolean> modifyAccountAmount(Long id,double nm){
        int result = accountRepository.modifyAccountAmount(id, nm);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("修改账户金额失败");
    }

    /**
     * 修改金币余额
     * @param id
     * @param nm
     * @return
     */
    public ResponseEntity<Boolean> modifyCoinAmount(Long id,double nm){
        int result = accountRepository.modifyCoinAmount(id, nm);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("修改金币余额失败");
    }
}
